using MediatR;
using Microsoft.AspNetCore.Mvc;
using RBAC.ErrorCode;
using SmartHealthcare.Read.Api.Application.Command.Payment;
using SmartHealthcare.Read.Api.DTo;
using SmartHealthcare.Read.Api.Services;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace SmartHealthcare.Read.Api.Controllers
{
    /// <summary>
    /// 支付记录查询控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PaymentRecordController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly ILogger<PaymentRecordController> _logger;
       

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator">中介者</param>
        /// <param name="logger">日志记录器</param>
        /// <param name="pdfExportService">PDF导出服务</param>
        public PaymentRecordController(
            IMediator mediator,
            ILogger<PaymentRecordController> logger
        )
        {
            _mediator = mediator;
            _logger = logger;
           
        }

        /// <summary>
        /// 获取支付记录列表
        /// </summary>
        /// <param name="inpatientRegistryId">住院号（住院登记ID）</param>
        /// <param name="medicalCard">病人卡号</param>
        /// <param name="patientName">患者姓名</param>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="paymentMethod">支付方式</param>
        /// <param name="paymentType">支付类型</param>
        /// <returns>支付记录列表</returns>
        [HttpGet("list")]
        public async Task<APIResult<List<PaymentRecordDto>>> GetPaymentRecords(
            [FromQuery] long? inpatientRegistryId,
            [FromQuery] string medicalCard,
            [FromQuery] string patientName,
            [FromQuery] DateTime? startTime,
            [FromQuery] DateTime? endTime,
            [FromQuery] string paymentMethod,
            [FromQuery] string paymentType)
        {
            _logger.LogInformation("接收到获取支付记录列表请求");

            var command = new GetPaymentRecordsCommand
            {
                InpatientRegistryId = inpatientRegistryId,
                MedicalCard = medicalCard,
                PatientName = patientName,
                StartTime = startTime,
                EndTime = endTime,
                PaymentMethod = paymentMethod,
                PaymentType = paymentType
            };

            return await _mediator.Send(command);
        }

        /// <summary>
        /// 获取支付记录详情
        /// </summary>
        /// <param name="id">支付记录ID</param>
        /// <returns>支付记录详情</returns>
        [HttpGet("{id}")]
        public async Task<APIResult<PaymentRecordDto>> GetPaymentRecordDetail(long id)
        {
            _logger.LogInformation($"接收到获取支付记录详情请求，ID：{id}");

            var command = new GetPaymentRecordDetailCommand
            {
                Id = id
            };

            return await _mediator.Send(command);
        }

        ///// <summary>
        ///// 导出PDF格式报告
        ///// </summary>
        ///// <param name="inpatientRegistryId">住院号（住院登记ID）</param>
        ///// <returns>PDF文件流</returns>
        //[HttpGet("export-pdf")]
        //public async Task<IActionResult> ExportPaymentRecordsPdf(long inpatientRegistryId)
        //{
        //    _logger.LogInformation($"接收到导出支付记录PDF请求，住院登记ID：{inpatientRegistryId}");

        //    try
        //    {
        //        // 获取支付记录列表
        //        var command = new GetPaymentRecordsCommand
        //        {
        //            InpatientRegistryId = inpatientRegistryId
        //        };

        //        var result = await _mediator.Send(command);

        //        if (result.Code != APIEnums.Success || result.Data == null || result.Data.Count == 0)
        //        {
        //            return NotFound("未找到相关支付记录");
        //        }

        //        // 获取患者信息
        //        string patientName = result.Data.FirstOrDefault()?.PatientName ?? "未知患者";
        //        string medicalCard = result.Data.FirstOrDefault()?.MedicalCard ?? "未知卡号";
                
        //        // 生成PDF
        //        var pdfBytes = await _pdfExportService.GeneratePaymentRecordsPdf(
        //            result.Data, 
        //            patientName, 
        //            medicalCard);
                
        //        // 返回PDF文件
        //        return File(
        //            pdfBytes, 
        //            "application/pdf", 
        //            $"支付记录_{patientName}_{inpatientRegistryId}_{DateTime.Now:yyyyMMddHHmmss}.pdf");
        //    }
        //    catch (Exception ex)
        //    {
        //        _logger.LogError(ex, $"导出支付记录PDF时发生异常，住院登记ID：{inpatientRegistryId}");
        //        return StatusCode(500, $"导出PDF失败：{ex.Message}");
        //    }
        //}
    }
} 